import { reviewhouseAPI } from "../../../api/houseapi"
import { repairItemAPI ,formrepairAPI,getdetailrepairAPI } from "../../../api/repair"
import { uploadAPI } from "../../../api/public"
// 在页面或组件中导入
import validate from 'wechat-validate'
Page({
  // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    //1
    id:undefined,
    //1房屋id
    houseId:'',
    //1维修id
    repairItemId:'',
    //1手机号
    mobile:'',
    // 预约日期
    appointment:'',
    //问题描述
    description:'',
    // 图片数组
    attachment: [
      // { url: '/repair_pkg/static/uploads/attachment.jpg' },
      // { url: '/repair_pkg/static/uploads/attachment.jpg' },
    ],
    houseList: [],
    houseInfo:'',
    repairItem: [],
    repairItemName: '',
  },
  rules: {
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1\d{10}$/, message: '请检查手机号码是否正确!' },
    ],
    description: [
      { required: true, message: '请填写问题描述!' },
    ]
  },
  // 打开关闭保修房屋
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 打开关闭维修项目
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({repairLayerVisible: false, })
  },
  // 打开关闭保修日期
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 提交报修
  async goList() {
    const isAllValid = this.validate()
    if(!isAllValid) return
    console.log(123);
    const {id,houseId,repairItemId,mobile,appointment,description,attachment} = this.data
    const form = {id,houseId,repairItemId,mobile,appointment,description,attachment}
    console.log(form)
    await formrepairAPI(form)
    wx.utils.toast('提交保修成功')
    setTimeout(() => {
      wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
    }, 2000);
  },

  // 自己写的代码
  onLoad (ev) {
    this.reviewhouse()
    this.repairItem()
    console.log('123123',ev.id);
    const id = ev.id
    wx.setNavigationBarTitle({
      title: id ? '修改报修信息' : '填写报修信息',
    })
    if (id) {
      this.getdetailrepair(id)
    }
  },
  // 编辑回显数据
   async getdetailrepair (id){
    const res = await getdetailrepairAPI(id)
    console.log(res)
    const {...data} = res.data
    this.setData({
      ...data,
    })
   },
  // 查询审核过的房屋
  async reviewhouse() {
    const res = await reviewhouseAPI()
    console.log(res)
    this.setData({
      houseList : res.data.map(item => {
        return { name : item.name,id:item.id}
      })
    })
  },
  // 渲染审核过的房屋
  seletchouse(e) {
    console.log(e.detail.name);
    this.setData({
      houseInfo:e.detail.name,
      houseId:e.detail.id
    })
  },
  // 查询维修项目
  async repairItem() {
    const res  = await repairItemAPI()
    console.log(res.data);
    this.setData({
      repairItem : res.data.map(item => {
        return {name:item.name,id:item.id}
      })
    })
  },
  // 渲染维修项目
  seletcrepair(e) {
    console.log(e.detail.name);
    this.setData({
      repairItemName : e.detail.name,
      repairItemId : e.detail.id
    })
  },
  // 通过组件获取时间戳
  confirmdata(e) {
    console.log(e.detail);
    const res = wx.utils.timedata(e.detail,'-')
    this.setData({
      appointment:res,
      dateLayerVisible: false
    })
  },
  // 上传图片校验
  beforeRead(e) {
    const {file,callback } = e.detail
    if (file.size > 1024 * 1024 * 1) {
      wx.utils.toast('上传文件小于1m')
      callback(false)
    }
    if (file.fileType !== 'image') {
      wx.utils.toast('请选择图片')
      callback(false)
    }
    callback(true)
  },
  // 上传图片
  async uploadimg(e) {
    const {file} = e.detail
    console.log(file,12312312312);
    const res = await uploadAPI(file.url)
    console.log(res);
    this.setData({
      attachment:[...this.data.attachment,res.data]
    })
    
  },
  // 删除图片
  deleteimg(e) {
    console.log(e);
    const {index} = e.detail
    this.setData({
      attachment : this.data.attachment.filter((item,i) => index !==i )
    })
  },
})

